package com.wanlian.util;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;


/**
 * @param <T>
 * @author holll7
 */
public class QueueUtil<T> {

    //todo:设置自动协作的二级缓存结构
    private Queue<T> queue = new LinkedList<T>();

    public synchronized void add(T t) {
        if (queue != null) {
            queue.offer(t);
        }
    }

    public synchronized T get() {
        if (queue != null) {
            if (queue.size() > 0) {
                T t = queue.poll();
                return t;
            }
        }
        return null;
    }

    public synchronized List<T> get(int count) {
        if (queue != null) {
            List<T> list = new ArrayList<T>();
            int size = queue.size();
            count = count > size ? size : count;
            for (int i = 0; i < count; i++) {
                T item = queue.poll();
                list.add(item);
            }
            return list;
        }
        return null;
    }


    public synchronized long size() {
        return this.queue.size();
    }


}
